package top.malaoshi;
import com.yingqi.entity.PostUser;
import com.yingqi.mapper.IPostMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;
import com.yingqi.entity.User;
import com.yingqi.mapper.IUserMapper;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class Main {
    @Test
    public void test() throws IOException {
        /*
         1. 读取全局配置文件.
            getResourceAsStream 使用的资源路径
         */
        InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml");
        // 2. 使用sqlSession的工厂构造器来读入全局配置，再生成一个sqlSession工厂.
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        // 3. 通过 sqlSession 工厂打开一个 sqlSession
        SqlSession sqlSession = sqlSessionFactory.openSession();
        try {
            //4 使用SqlSession创建Dao接口的代理对象
            IUserMapper mapper = sqlSession.getMapper(IUserMapper.class);
            List<User> list=mapper.selectAll();
            for(User user:list){
                String info= String.format("id：%s,用户名：%s,密码：%s",user.getId(),user.getUsername(),user.getPassword() );
                System.out.println(info);
            }
        }finally{
            sqlSession.close();
        }
    }
    @Test
    public void testSelect() throws IOException {
        User user=new User();
        user.setUsername("李%");
        user.setPassword("12345678");
        InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        IUserMapper mapper = sqlSession.getMapper(IUserMapper.class);

        User userparam=new User();
        userparam.setUsername("李%");
        List<User> list=mapper.select(userparam);
        for(User us:list) {
            String info = String.format("id：%s,用户名：%s,密码：%s", us.getId(), us.getUsername(), us.getPassword());
            System.out.println(info);
        }
        sqlSession.close();
        inputStream.close();
    }
    @Test
    public void updateUser() throws IOException {
        /*
         1. 读取全局配置文件.
            getResourceAsStream 使用的资源路径
         */
        InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml");
        // 2. 使用sqlSession的工厂构造器来读入全局配置，再生成一个sqlSession工厂.
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        // 3. 通过 sqlSession 工厂打开一个 sqlSession
        SqlSession sqlSession = sqlSessionFactory.openSession();
        try {
            //4 使用SqlSession创建Dao接口的代理对象
            IUserMapper mapper = sqlSession.getMapper(IUserMapper.class);
            User userParam=new User();
            userParam.setId("10001");
            userParam.setPassword("1234");
            int n= mapper.updateUser(userParam);
            System.out.println("更新 "+n+" 条记录");
            sqlSession.commit();
        }catch (Exception e){
            sqlSession.rollback();
            e.printStackTrace();
        }finally {
            sqlSession.close();
        }
    }
    @Test
    public void deleteByIds() throws IOException {
        /*
         1. 读取全局配置文件.
            getResourceAsStream 使用的资源路径
         */
        InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml");
        // 2. 使用sqlSession的工厂构造器来读入全局配置，再生成一个sqlSession工厂.
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        // 3. 通过 sqlSession 工厂打开一个 sqlSession
        SqlSession sqlSession = sqlSessionFactory.openSession();
        try {
            //4 使用SqlSession创建Dao接口的代理对象
            IUserMapper mapper = sqlSession.getMapper(IUserMapper.class);
            String[] ids={"10001","10002","10003"};
            int n= mapper.deleteByIds(ids);
            System.out.println("更新 "+n+" 条记录");
            sqlSession.commit();
        }catch (Exception e){
            sqlSession.rollback();
            e.printStackTrace();
        }finally {
            sqlSession.close();
        }
    }
    @Test
    public void Selectquery() throws IOException {
        InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        IPostMapper mapper = sqlSession.getMapper(IPostMapper.class);
        List<Map> list=mapper.query();
        for(Map map:list) {
            String info = String.format("id：%s,作者：%s,标题：%s", map.get("id"), map.get("username"), map.get("title"));
            System.out.println(info);
        }
        sqlSession.close();
        inputStream.close();
    }
    @Test
    public void selectPostUser() throws IOException {
        /*
         1. 读取全局配置文件.
            getResourceAsStream 使用的资源路径
         */
        InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml");
        // 2. 使用sqlSession的工厂构造器来读入全局配置，再生成一个sqlSession工厂.
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        // 3. 通过 sqlSession 工厂打开一个 sqlSession
        SqlSession sqlSession = sqlSessionFactory.openSession();
        try {
            //4 使用SqlSession创建Dao接口的代理对象
            IPostMapper mapper = sqlSession.getMapper(IPostMapper.class);
            List<PostUser> list = mapper.queryList();
            for (PostUser pu : list) {
                String info = String.format("id：%s,标题：%s,作者：%s", pu.getId(),pu.getTitle(),pu.getUsername());
                System.out.println(info);
            }
        } finally {
            sqlSession.close();
        }
    }
}
